﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Projectmanagement.Entity;
using Projectmanagement.Utl;
using ProjectManagement.Models;
using Telerik.Web.Mvc;

namespace Projectmanagement.Controllers
{
    [RoleRequired]
    public class TaskTemplateController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult _SelectIndex()
        {
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", null)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SaveAjaxEditing(int id)
        {
            var item = CH.GetDataByID<TaskTemplate>(id);
            if (TryUpdateModel(item))
            {
                CH.Edit<TaskTemplate>(item);
            }
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", null)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _InsertAjaxEditing()
        {
            var item = new TaskTemplate();

            if (TryUpdateModel(item))
            {

                CH.Create<TaskTemplate>(item);
            }
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", null)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            CH.Delete<TaskTemplate>(id);
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", null)));
        }

        [HttpPost]
        public ActionResult GetSimpleTask(int id)
        {
            var task = CH.DB.TaskTemplates.Find(id);
            return View(task);
        }

        [GridAction]
        public ActionResult _SelectIndexMilestoneProject(int? conferenceid, int? projectid)
        {
            var task = (from x in CH.DB.Tasks
                        where x.ProjectID == projectid
                                select x).Distinct().ToList();
            
            var temps = DQ.GetTaskTemplatesSearch(null, "", conferenceid).ToList();
            var exceptE = temps.Where(a => !task.Select(b => b.Name).Contains(a.Name));
           //var tt= temps.Select(x => x.Name).Except(task.Select(x => x.Name)).Select(x => temps.Single(y => y.Name == x));


           // var query = temps.Where(temp => !task.Select(x => x.Name).Contains(temp.Name));
            return View(new GridModel(exceptE));
        }


        [GridAction]
        public ActionResult _SelectIndexMilestone(int? conferenceid)
        {
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", conferenceid)));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _SaveAjaxEditingMilestone(int id, int? conferenceid)
        {
            var item = CH.GetDataByID<TaskTemplate>(id);
            item.MilestoneID = conferenceid;
            if (TryUpdateModel(item))
            {
                CH.Edit<TaskTemplate>(item);
            }
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", conferenceid)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _InsertAjaxEditingMilestone(int? conferenceid)
        {
            var item = new TaskTemplate();
            item.MilestoneID = conferenceid;
            if (TryUpdateModel(item))
            {
                CH.Create<TaskTemplate>(item);
            }
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", conferenceid)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditingMilestone(int id, int? conferenceid)
        {
            CH.Delete<TaskTemplate>(id);
            return View(new GridModel(DQ.GetTaskTemplatesSearch(null, "", conferenceid)));
        }


        public JsonResult GetMilestone()
        {
            var milestone = from m in CH.DB.MilestoneTemplate
                         select m;
            var selectList = milestone.ToList().Select(a => new SelectListItem
            {
                Text = a.MilestoneName,
                Value = a.ID.ToString()
            });
            return Json(selectList, JsonRequestBehavior.AllowGet);
        }
    }
}
